package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Ticket;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TicketQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITicketService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/regions")
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ServletContext servletContext;//上下文路径
    @Autowired
    private ITicketService ticketService;

    /**
     * 根据状态查询 一系列的地区
     * @param state : 状态
     * @return
     */
    @GetMapping
    public List list(Long state){
        List<Region> list=regionService.listAll(state);
        return list;
    }

    /**
     * 查询某地区的推荐攻略及全部攻略
     * 以response流的形式响应获取-->> html
     * @param qo : 地区id,状态
     */
    @GetMapping(value = "/{regionId}/strategies",produces = "text/html;charset=utf-8")
    public void queryStrategyByRegionBy(StrategyQueryObject qo, HttpServletResponse response){

        //查询所有的攻略
        qo.setState(null);//所有的状态
        PageInfo<Strategy> all = strategyService.query(qo);

        //查询推荐攻略
        qo.setState(Region.STATE_HOT);//推荐状态
        qo.setPageSize(0);//不分页 
        PageInfo<Strategy> hot = strategyService.query(qo);


        //利用freemarker把模版与数据合成
        //创建freemarker的配置对象
        Configuration configuration=new Configuration(Configuration.VERSION_2_3_23);
        //设置模版目录
        try {
            configuration.setDirectoryForTemplateLoading(new File(servletContext.getRealPath("/template")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置编码格式
        configuration.setDefaultEncoding("utf-8");

        Template template=null;
        //获取模板
        try {
            template = configuration.getTemplate("strategyTemplate.ftl");
        } catch (IOException e) {
            e.printStackTrace();
        }

        //打包数据
        Map<String,Object> map=new HashMap<>();
        map.put("hot",hot.getList());
        map.put("all",all.getList());

        //响应格式
        response.setContentType("text/html;charset=utf-8");

        //模板与数据的合成
        try {
            template.process(map,response.getWriter());
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

        @GetMapping(value = "/{regionId}/strategies",produces = "application/json;charset=utf-8")
        public PageInfo queryStrategyByRegionBy(StrategyQueryObject qo){
        return strategyService.query(qo);
    }

        //查询是推荐状态的景点门票
        @GetMapping("/commendTicket")
        public PageInfo queryCommendTicket(TicketQueryObject qo){
            return ticketService.query(qo);
        }

        //查询所有景点
        @GetMapping("/getScenic")
        public List<Ticket> getScenic(){
            return ticketService.listAll();
        }

}
